<?php

/**
 * Utility function to return a value from a named array or a specified default
 * @param array A named array
 * @param string The key to search for
 * @param mixed The default value to give if no key found
 * @param int An options mask: _MOS_NOTRIM prevents trim, _MOS_ALLOWHTML allows safe html, _MOS_ALLOWRAW allows raw input
 */
define ( "_MOS_NOTRIM", 0x0001 );
define ( "_MOS_ALLOWHTML", 0x0002 );
define ( "_MOS_ALLOWRAW", 0x0004 );

abstract class router
{
	static function getParam($name, $def = null, &$arr=null, $mask = 0) {
		static $noHtmlFilter = null;
		static $safeHtmlFilter = null;
		
		$return = null;
		if($arr==null) {
			$arr = &$_REQUEST;
		}
		if (isset ( $arr [$name] )) {
			$return = $arr [$name];
			
			if (is_string ( $return )) {
				// trim data
				if (! ($mask & _MOS_NOTRIM)) {
					$return = trim ( $return );
				}
				
				if ($mask & _MOS_ALLOWRAW) {
					// do nothing
				} else if ($mask & _MOS_ALLOWHTML) {
					// do nothing - compatibility mode
				} else {
					if (! empty ( $return ) && is_numeric ( $def )) {
						// if value is defined and default value is numeric set variable type to integer
						$return = intval ( $return );
					}
				}
				
				// account for magic quotes setting
				if (! get_magic_quotes_gpc ()) {
					$return = addslashes ( $return );
				}
			}
			
			return $return;
		} else {
			return $def;
		}
	}
	static function redirect($link='') {
		$link = $link ? $link : self::$root;
		if (headers_sent ()) {
			echo "<script>document.location.href='$link';</script>\n";
		} else {
			header ( 'HTTP/1.1 301 Moved Permanently' );
			header ( "Location: " . $link );
		}
		exit();
	}
	static function encode($string) {
	}
	static function decode($string) {
	}
}

?>